﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page 
{
    HttpCookie cookie1 ;
    HttpCookie cookie2 ;

    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Request.Cookies["user"] != null)
        {
            ReadCookie();
            form1.Visible = false;
        }
        else
        {
            Label1.Text = "welome guest. <br><hr>";
        }

    }

    protected void WriteClicked(Object Sender, EventArgs e)
    {
        //Create a new cookie, passing the name into the constructor
        HttpCookie cookie1 = new HttpCookie("user");

        //Set the cookies value
        cookie1.Value = User.Text;

        //Set the cookie to expire in 1 minute
        DateTime dtNow = DateTime.Now;
        TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0);
        cookie1.Expires = dtNow + tsMinute;
       

        //Add the cookie
        Response.Cookies.Add(cookie1);

        /////////////////////
        HttpCookie cookie2 = new HttpCookie("pass");

        //Set the cookies value
        cookie2.Value = Pass.Text;

        //Set the cookie to expire in 1 minute
        cookie2.Expires = dtNow + tsMinute;

        //Add the cookie
        Response.Cookies.Add(cookie2);
        //reload page
        //Response.Redirect("Default.aspx");
        ////////
        

 string javaScript =
  "<script language=JavaScript>\n" +
  "Button2_onclick();\n" +
  "</script>";
 
 RegisterStartupScript("Button2_ClickScript", javaScript);
 ReadCookie();

    ////////////////
        
      
    }
    protected void ReadCookie()
    {
        //Get the cookie name the user entered
        //String strCookieName = Request.QueryString["user"].ToString();
        String strCookieUser = "user";
        String strCookiePass = "pass";
        //Grab the cookie
       cookie1 = Request.Cookies[strCookieUser];
       cookie2 = Request.Cookies[strCookiePass];

        //Check to make sure the cookie exists
        if (cookie1 == null)
        {
            Response.Write("Cookie not found. <br><hr>"); 
        }
        else
        {
            //Write the cookie value
            String strCookieUserValue = cookie1.Value.ToString();
            String strCookiePassValue = cookie2.Value.ToString();
            string welcome = "welcome " + strCookieUserValue + "</b><br><hr>";
            Label1.Text=welcome;
            welcome = "your pass is: " + strCookiePassValue + "</b><br><hr>";
           // Label1.Text = welcome;
        }
    }
  
    protected void Button2_Click1(object sender, EventArgs e)
    {
        cookie1.Expires = DateTime.Now;
        cookie2.Expires = DateTime.Now;
    }
}
